//
//  ChouJiangViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/22.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//***********积分说明***********
#import "ChouJiangViewController.h"
#import "UIView+LongMessageView.h"


@interface ChouJiangViewController ()<UIAlertViewDelegate>
@property(nonatomic,copy)UILabel *l1;
@property(nonatomic,copy)UILabel *l2;
@property(nonatomic,copy)UILabel *l3;
@property(nonatomic,copy)UILabel *l4;
@property(nonatomic,copy)UILabel *l5;
@property(nonatomic,copy)UILabel *l6;
@property(nonatomic,copy)UILabel *l7;
@property(nonatomic,copy)UILabel *l8;
@property(nonatomic,copy)UILabel *l9;
@property(nonatomic,copy)UILabel *l10;
@property(nonatomic,copy)UIButton *startBtn;
@property(nonatomic,assign)NSTimer *myTimer;
@property(nonatomic,assign)NSInteger myTime;
@property(nonatomic,copy)UIView *moveView;
@property(nonatomic,copy)NSArray *dataArray;
@property(nonatomic,assign)NSTimeInterval endTime;
@property(nonatomic,assign)NSInteger selectedIndex;
@property(nonatomic,copy)NSArray *allData;


@property(nonatomic,copy)UIView *v1;
@property(nonatomic,copy)UIView *v2;
@property(nonatomic,copy)UIView *v3;
@property(nonatomic,copy)UIView *v4;
@property(nonatomic,copy)UIView *v5;
@property(nonatomic,copy)UIView *v6;
@property(nonatomic,copy)UIView *v7;
@property(nonatomic,copy)UIView *v8;
@property(nonatomic,copy)UIView *v9;
@property(nonatomic,copy)UIView *v10;


/**
 *提示View
 */
@property(nonatomic,copy)UIView *attentionView;

@property(nonatomic,copy)UITextField *moneyTextField;

@property(nonatomic,assign)NSInteger freeChance;

@property(nonatomic,assign)CGFloat myAllScore;

@end

@implementation ChouJiangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    _allData=[NSArray array];
    
    [self initView];
    [self createNavigation];
    _moveView=[[UIView alloc]init];
    CGFloat lab_w=(self.view.frame.size.width-20*2-10*3)/4.0;
    CGFloat lab_h=lab_w;
    _moveView.frame=CGRectMake(20, self.view.frame.size.height-30-(lab_h+10)*3, lab_w, lab_h);
    [self createUI];
    [self getChouJiangData];
    NSLog(@"进入");
    // Do any additional setup after loading the view.
}
-(void)initView
{
    _v1=[[UIView alloc]init];
    _v1.backgroundColor=[UIColor grayColor];
    _v1.alpha=0.3;
    _v1.layer.cornerRadius=15.0;
    _v1.layer.borderWidth=1.0;
    _v1.layer.borderColor=[UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:208/255.0].CGColor;
    
    
    _v2=[[UIView alloc]init];
    _v2.backgroundColor=[UIColor grayColor];
    _v2.alpha=0.3;
    _v2.layer.cornerRadius=15.0;
    _v2.layer.borderWidth=1.0;
    _v2.layer.borderColor=[UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:208/255.0].CGColor;
    
    
    _v3=[[UIView alloc]init];
    _v3.backgroundColor=[UIColor grayColor];
    _v3.alpha=0.3;
    _v3.layer.cornerRadius=15.0;
    _v3.layer.borderWidth=1.0;
    _v3.layer.borderColor=[UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:208/255.0].CGColor;
    
    
    _v4=[[UIView alloc]init];
    _v4.backgroundColor=[UIColor grayColor];
    _v4.alpha=0.3;
    _v4.layer.cornerRadius=15.0;
    _v4.layer.borderWidth=1.0;
    _v4.layer.borderColor=[UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:208/255.0].CGColor;
    
    _v5=[[UIView alloc]init];
    _v5.backgroundColor=[UIColor grayColor];
    _v5.alpha=0.3;
    _v5.layer.cornerRadius=15.0;
    _v5.layer.borderWidth=1.0;
    _v5.layer.borderColor=[UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:208/255.0].CGColor;
    
    _v6=[[UIView alloc]init];
    _v6.backgroundColor=[UIColor grayColor];
    _v6.alpha=0.3;
    _v6.layer.cornerRadius=15.0;
    _v6.layer.borderWidth=1.0;
    _v6.layer.borderColor=[UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:208/255.0].CGColor;
    
    _v7=[[UIView alloc]init];
    _v7.backgroundColor=[UIColor grayColor];
    _v7.alpha=0.3;
    _v7.layer.cornerRadius=15.0;
    _v7.layer.borderWidth=1.0;
    _v7.layer.borderColor=[UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:208/255.0].CGColor;
    
    _v8=[[UIView alloc]init];
    _v8.backgroundColor=[UIColor grayColor];
    _v8.alpha=0.3;
    _v8.layer.cornerRadius=15.0;
    _v8.layer.borderWidth=1.0;
    _v8.layer.borderColor=[UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:208/255.0].CGColor;
    
    
    _v9=[[UIView alloc]init];
    _v9.backgroundColor=[UIColor grayColor];
    _v9.alpha=0.3;
    _v9.layer.cornerRadius=15.0;
    _v9.layer.borderWidth=1.0;
    _v9.layer.borderColor=[UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:208/255.0].CGColor;
    
    _v10=[[UIView alloc]init];
    _v10.backgroundColor=[UIColor grayColor];
    _v10.alpha=0.3;
    _v10.layer.cornerRadius=15.0;
    _v10.layer.borderWidth=1.0;
    _v10.layer.borderColor=[UIColor colorWithRed:208/255.0 green:208/255.0 blue:208/255.0 alpha:208/255.0].CGColor;
    
    
}

//初始化view的frame
-(void)initViewFrame
{
    _v1.frame=_l1.frame;
    _v2.frame=_l2.frame;
    _v3.frame=_l3.frame;
    _v4.frame=_l4.frame;
    _v5.frame=_l5.frame;
    _v6.frame=_l6.frame;
    _v7.frame=_l7.frame;
    _v8.frame=_l8.frame;
    _v9.frame=_l9.frame;
    _v10.frame=_l10.frame;
    [self.view addSubview:_v1];
    [self.view addSubview:_v2];
    [self.view addSubview:_v3];
    [self.view addSubview:_v4];
    [self.view addSubview:_v5];
    [self.view addSubview:_v6];
    [self.view addSubview:_v7];
    [self.view addSubview:_v8];
    [self.view addSubview:_v9];
    [self.view addSubview:_v10];
    //[self viewAddSubView];
}

//往View上贴视图
-(void)viewAddSubView
{
    CGFloat lab_w=(self.view.frame.size.width-20*2-10*3)/4.0;
    CGFloat lab_h=lab_w;
    UIImageView *img1=[[UIImageView alloc]initWithFrame:CGRectMake(2, lab_h/4.0, lab_h/2.0, lab_h/2.0)];
    img1.image=[UIImage imageNamed:@"signIn_btn4"];
    [_v1 addSubview:img1];
}
-(void)createNavigation
{
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"抽奖";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
    
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=NO;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 编写界面
-(void)createUI{
    CGFloat lab_w=(self.view.frame.size.width-20*2-10*3)/4.0;
    CGFloat lab_h=lab_w;
    _l1=[[UILabel alloc]initWithFrame:CGRectMake(20, self.view.frame.size.height-30-(lab_h+10)*3, lab_w, lab_h)];
    //_l1.backgroundColor=[UIColor grayColor];
    _l1.text=@"谢谢参与";
    _l1.textAlignment=NSTextAlignmentCenter;
    _l1.textColor=[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1];
    _l1.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:_l1];
    
    _l2=[[UILabel alloc]initWithFrame:CGRectMake(20+lab_w+10, self.view.frame.size.height-30-(lab_h+10)*3, lab_w, lab_h)];
    //_l2.backgroundColor=[UIColor grayColor];
    _l2.text=@"5积分";
    _l2.textAlignment=NSTextAlignmentCenter;
    _l2.textColor=[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1];
    _l2.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:_l2];
    
    
    _l3=[[UILabel alloc]initWithFrame:CGRectMake(20+(lab_w+10)*2, self.view.frame.size.height-30-(lab_h+10)*3, lab_w, lab_h)];
    //_l3.backgroundColor=[UIColor grayColor];
    _l3.text=@"30积分";
    _l3.textAlignment=NSTextAlignmentCenter;
    _l3.textColor=[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1];
    _l3.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:_l3];
    
    _l4=[[UILabel alloc]initWithFrame:CGRectMake(20+(lab_w+10)*3, self.view.frame.size.height-30-(lab_h+10)*3, lab_w, lab_h)];
    //_l4.backgroundColor=[UIColor grayColor];
    _l4.text=@"60积分";
    _l4.textAlignment=NSTextAlignmentCenter;
    _l4.textColor=[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1];
    _l4.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:_l4];
    
    _l5=[[UILabel alloc]initWithFrame:CGRectMake(20+(lab_w+10)*3, self.view.frame.size.height-30-(lab_h+10)*3+lab_h+10, lab_w, lab_h)];
    //_l5.backgroundColor=[UIColor grayColor];
    _l5.text=@"5元现金";
    _l5.textAlignment=NSTextAlignmentCenter;
    _l5.textColor=[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1];
    _l5.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:_l5];
    
    _l6=[[UILabel alloc]initWithFrame:CGRectMake(20+(lab_w+10)*3, self.view.frame.size.height-30-(lab_h+10)*3+(lab_h+10)*2, lab_w, lab_h)];
    //_l6.backgroundColor=[UIColor grayColor];
    _l6.text=@"免单60元";
    _l6.textAlignment=NSTextAlignmentCenter;
    _l6.textColor=[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1];
    _l6.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:_l6];
    
    _l7=[[UILabel alloc]initWithFrame:CGRectMake(20+(lab_w+10)*2, self.view.frame.size.height-30-(lab_h+10)*3+(lab_h+10)*2, lab_w, lab_h)];
    //_l7.backgroundColor=[UIColor grayColor];
    _l7.text=@"免单150元";
    _l7.textAlignment=NSTextAlignmentCenter;
    _l7.textColor=[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1];
    _l7.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:_l7];
    
    _l8=[[UILabel alloc]initWithFrame:CGRectMake(20+(lab_w+10)*1, self.view.frame.size.height-30-(lab_h+10)*3+(lab_h+10)*2, lab_w, lab_h)];
    //_l8.backgroundColor=[UIColor grayColor];
    _l8.text=@"8折优惠券";
    _l8.textAlignment=NSTextAlignmentCenter;
    _l8.textColor=[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1];
    _l8.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:_l8];
    
    _l9=[[UILabel alloc]initWithFrame:CGRectMake(20+(lab_w+10)*0, self.view.frame.size.height-30-(lab_h+10)*3+(lab_h+10)*2, lab_w, lab_h)];
    //_l9.backgroundColor=[UIColor grayColor];
    _l9.text=@"5折优惠券";
    _l9.textAlignment=NSTextAlignmentCenter;
    _l9.textColor=[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1];
    _l9.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:_l9];
    
    _l10=[[UILabel alloc]initWithFrame:CGRectMake(20+(lab_w+10)*0, self.view.frame.size.height-30-(lab_h+10)*3+(lab_h+10)*1, lab_w, lab_h)];
    //_l10.backgroundColor=[UIColor grayColor];
    _l10.text=@"免费检测";
    _l10.textAlignment=NSTextAlignmentCenter;
    _l10.textColor=[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1];
    _l10.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:_l10];
    
    [self initViewFrame];
    
    //开始抽奖按钮
    _startBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    _startBtn.backgroundColor=[UIColor colorWithRed:220/255.0 green:58/255.0 blue:23/255.0 alpha:1];
    _startBtn.titleLabel.font=[UIFont systemFontOfSize:30];
    _startBtn.layer.cornerRadius=10.0;
    _startBtn.layer.borderWidth=1.0;
    _startBtn.layer.borderColor=[UIColor colorWithRed:220/255.0 green:58/255.0 blue:23/255.0 alpha:1].CGColor;
    _startBtn.frame=CGRectMake(20+(lab_w+10)*1, self.view.frame.size.height-30-(lab_h+10)*3+(lab_h+10)*1, lab_w*2+10, lab_h);
    [_startBtn setTitle:@"开始抽奖" forState:UIControlStateNormal];
    [_startBtn setTitleColor:[UIColor colorWithRed:242/255.0 green:239/255.0 blue:146/255.0 alpha:1] forState:UIControlStateNormal];
    [_startBtn addTarget:self action:@selector(startBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_startBtn];
    
    //移动View
    _moveView=[[UIView alloc]initWithFrame:_l1.frame];
    _moveView.backgroundColor=[UIColor redColor];
    _moveView.layer.cornerRadius=15.0;
    _moveView.layer.borderWidth=1.0;
    _moveView.alpha=0.4;
    _moveView.layer.borderColor=[UIColor colorWithRed:210/255.0 green:105/255.0 blue:106/255.0 alpha:1].CGColor;
    [self.view addSubview:_moveView];
    
    //初始化数据
    _dataArray=@[_l1,_l2,_l3,_l4,_l5,_l6,_l7,_l8,_l9,_l10];
    //[self initData];
    
    
    
    //今日抽奖
    UIButton *todayLottery=[UIButton buttonWithType:UIButtonTypeCustom];
    todayLottery.frame=CGRectMake((self.view.frame.size.width*0.4)/2.0, self.view.frame.size.height-30-(lab_h+10)*3-10-50, self.view.frame.size.width*0.6, 50);
    todayLottery.backgroundColor=[UIColor colorWithRed:220/255.0 green:58/255.0 blue:23/255.0 alpha:1];
    [todayLottery setTitle:@"今日抽奖" forState:UIControlStateNormal];
    todayLottery.titleLabel.font=[UIFont systemFontOfSize:30];
    todayLottery.layer.cornerRadius=10.0;
    todayLottery.layer.borderWidth=1.0;
    todayLottery.layer.borderColor=[UIColor colorWithRed:220/255.0 green:58/255.0 blue:23/255.0 alpha:1].CGColor;
    [self.view addSubview:todayLottery];
    
    //背景图片
    UIImageView *backGroundImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20+44, self.view.frame.size.width, todayLottery.frame.origin.y-20-44)];
    backGroundImageView.image=[UIImage imageNamed:@"choujiang_background"];
    [self.view addSubview:backGroundImageView];
    
    //抽奖前的提示
    _attentionView=[[UIView alloc]initWithFrame:CGRectMake(todayLottery.frame.origin.x, _startBtn.frame.origin.y-20, todayLottery.frame.size.width, _startBtn.frame.size.height+40)];
    _attentionView.backgroundColor=[UIColor colorWithRed:225/255.0 green:59/255.0 blue:23/255.0 alpha:1];
    UILabel *attentionLab=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, _attentionView.frame.size.width-10*2, 0)];
    attentionLab.text=@"每日只有一次免费抽奖的机会,本次抽奖将花费5积分";
    attentionLab.textColor=[UIColor whiteColor];
    attentionLab.font=[UIFont systemFontOfSize:15];
    attentionLab.numberOfLines=0;
    attentionLab.frame=CGRectMake(10, 10, attentionLab.frame.size.width, [attentionLab boundingRectWithSize:CGSizeMake(attentionLab.frame.size.width, 0)].height);
    [_attentionView addSubview:attentionLab];
    [self.view addSubview:_attentionView];
    
    
    CGFloat btn_w=(_attentionView.frame.size.width-30*3)/2.0;
    CGFloat btn_h=30;
    //取消
    UIButton *cancelBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame=CGRectMake(30, _attentionView.frame.size.height-20-30, btn_w, btn_h);
    cancelBtn.backgroundColor=[UIColor whiteColor];
    cancelBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor colorWithRed:230/255.0 green:133/255.0 blue:0 alpha:1] forState:UIControlStateNormal];
    cancelBtn.layer.borderColor=[UIColor colorWithRed:225/255.0 green:59/255.0 blue:23/255.0 alpha:1].CGColor;
    cancelBtn.layer.borderWidth=1.0;
    cancelBtn.layer.cornerRadius=5.0;
    [cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_attentionView addSubview:cancelBtn];
    //确定
    UIButton *sureBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame=CGRectMake(30+btn_w+30, _attentionView.frame.size.height-20-30, btn_w, btn_h);
    sureBtn.backgroundColor=[UIColor whiteColor];
    sureBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor colorWithRed:230/255.0 green:133/255.0 blue:0 alpha:1] forState:UIControlStateNormal];
    sureBtn.layer.borderColor=[UIColor colorWithRed:225/255.0 green:59/255.0 blue:23/255.0 alpha:1].CGColor;
    sureBtn.layer.borderWidth=1.0;
    sureBtn.layer.cornerRadius=5.0;
    [sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_attentionView addSubview:sureBtn];
    _attentionView.hidden=YES;
    
}
//取消
-(void)cancelBtnClick:(UIButton *)sender
{
    _attentionView.hidden=YES;
    _startBtn.userInteractionEnabled=YES;
}
//确定
-(void)sureBtnClick:(UIButton *)sender
{
    if(_myAllScore>=5){
        [self startChouJiangData];
    }else{
        [self.view showTiShiMessage:@"积分不足，无法抽奖!"];
        return;
    }
    
//    _attentionView.hidden=YES;
//    //_dataArray=_allData[_selectedIndex];
//    _startBtn.userInteractionEnabled=NO;
//    _moveView.frame=_l1.frame;
//    NSInteger myRandom=arc4random()%10;
//    while (myRandom<0||myRandom>9) {
//        myRandom=arc4random()%10;
//    }
//    //NSLog(@"产生的随机数====%ld",myRandom);
//    myRandom=4;
//    _myTime=myRandom+70;
//    _endTime=0.9;
//    _myTimer=[NSTimer scheduledTimerWithTimeInterval:_endTime target:self selector:@selector(timerRun) userInfo:nil repeats:NO];

}
//#pragma mark 初始化数据
//-(void)initData
//{
//    _allData=@[@[_l1,_l2,_l3,_l4,_l5,_l6,_l7,_l8,_l9,_l10],@[_l2,_l3,_l4,_l5,_l6,_l7,_l8,_l9,_l10,_l1],@[_l3,_l4,_l5,_l6,_l7,_l8,_l9,_l10,_l1,_l2],@[_l4,_l5,_l6,_l7,_l8,_l9,_l10,_l1,_l2,_l3],@[_l5,_l6,_l7,_l8,_l9,_l10,_l1,_l2,_l3,_l4],@[_l6,_l7,_l8,_l9,_l10,_l1,_l2,_l3,_l4,_l5],@[_l7,_l8,_l9,_l10,_l1,_l2,_l3,_l4,_l5,_l6],@[_l8,_l9,_l10,_l1,_l2,_l3,_l4,_l5,_l6,_l7],@[_l9,_l10,_l1,_l2,_l3,_l4,_l5,_l6,_l7,_l8],@[_l10,_l1,_l2,_l3,_l4,_l5,_l6,_l7,_l8,_l9]];
//}
#pragma mark 开始抽奖
-(void)startBtnClick
{
    if (_freeChance==0) {
        [self startChouJiangData];
    }else{
        _attentionView.hidden=NO;
    }
    
}
static NSInteger a=0;
#pragma mark 启动定时器
-(void)timerRun
{
    a++;
    if (a<9) {
        _endTime-=0.1;
    }
    if (a>_myTime-6) {
        _endTime+=0.1;
    }
    if (a>_myTime) {
        [self showResultWithIndex:_myTime-70];
        //_selectedIndex=_myTime-70;
        a=0;
        _startBtn.userInteractionEnabled=YES;
        
    }else{
        [self viewStartMoveWithIndex:a%10];
        _myTimer=[NSTimer scheduledTimerWithTimeInterval:_endTime target:self selector:@selector(timerRun) userInfo:nil repeats:NO];
    }
    
}
#pragma mark view开始移动
-(void)viewStartMoveWithIndex:(NSInteger)index
{
    //NSLog(@"============%ld",index);
    UILabel *lab=(id)_dataArray[index];
    _moveView.frame=lab.frame;
}
#pragma mark 显示抽奖结果
-(void)showResultWithIndex:(NSInteger)index
{
    UILabel *lab=(id)_dataArray[index];
    if ([lab.text isEqualToString:@"5元现金"]) {
        [self payMoney];
    }else{
        NSString *str=[NSString stringWithFormat:@"恭喜你获得%@",lab.text];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"抽奖结果" message:str delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alert show];
    }
    
}
#pragma mark 输入支付宝或微信账号 发5元红包
-(void)payMoney
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"恭喜你获得5元现金" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.alertViewStyle=UIAlertViewStylePlainTextInput;
    _moneyTextField=[alert textFieldAtIndex:0];
    _moneyTextField.placeholder=@"用支付宝账号去领奖";
    [alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {
        //取消
    }else if(buttonIndex==1){
        //兑奖
        _moneyTextField=[alertView textFieldAtIndex:0];
        [self get5Yuan:_moneyTextField.text];
        
    }
}
//领取5元现金
-(void)get5Yuan:(NSString *)account
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    if (account==nil) {
        account=@"";
    }
    
    [manager POST:[NSString stringWithFormat:Get5YuanMoney,token] parameters:@{@"alipay_num":account} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"支付宝账号======%@dic====%@",account,dic);
        if (dic) {
            [self.view showLongMessageWithContent1:@"尊敬的用户:你好!" AndContent2:@"我们将于一个工作日内将红包发送到你的账户,感谢你的支持!"];
//            NSInteger state=[dic[@"status"][@"code"]integerValue];
//            if (state==0) {
//                
//            }else if (state==1){
//                
//            }
        }else{
            [self.view showTiShiMessage:@"程序员GG开了个小差"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
        [self.view showTiShiMessage:@"程序员GG开了个小差"];
    }];

}





-(void)viewWillDisappear:(BOOL)animated
{
    if (a!=0) {
        [_myTimer invalidate];
        _myTimer=nil;
    }

    a=0;
    _startBtn.userInteractionEnabled=YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//获取抽奖机会
-(void)getChouJiangData
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    [manager GET:[NSString stringWithFormat:GetFreeAward,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"获取当天免费抽奖机会%@",dic);
        _myAllScore=[dic[@"credits"]floatValue];
        _freeChance=[dic[@"have_luck"]integerValue];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
    }];
    
}

//开始抽奖
-(void)startChouJiangData
{
    _startBtn.userInteractionEnabled=NO;
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    [manager POST:[NSString stringWithFormat:StartChouJiang,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"抽奖的结果%@",dic);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSInteger prize=[dic[@"result"][@"prize"]integerValue];
            _attentionView.hidden=YES;
            _freeChance=[dic[@"have_luck"] integerValue];
            //_dataArray=_allData[_selectedIndex];
            _moveView.frame=_l1.frame;
            NSInteger myRandom;
            myRandom=prize-1;
            _myTime=myRandom+70;
            _endTime=0.9;
            _myTimer=[NSTimer scheduledTimerWithTimeInterval:_endTime target:self selector:@selector(timerRun) userInfo:nil repeats:NO];
        });
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"抽奖的结果error%@",error.localizedDescription);
        [self.view showTiShiMessage:@"网络不佳，无法抽奖!"];
        _startBtn.userInteractionEnabled=YES;
    }];
    
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
